package com.water.camera.util

import android.app.Activity
import android.app.RecoverableSecurityException
import android.content.ContentUris
import android.content.Intent
import android.content.IntentSender
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import androidx.annotation.Nullable
import androidx.fragment.app.FragmentActivity
import com.water.camera.fragment.ActivityResultFragment
import com.water.camera.fragment.FileUtilCallback
import java.io.File

object ImageUtil {

    /**
     * 删除文件
     * @return
     */
    fun deleteFileUri(activity: FragmentActivity, imgPath: String, callBack: FileUtilCallback) {
        val cursor: Cursor = MediaStore.Images.Media.query(
            activity.contentResolver,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            arrayOf<String>(MediaStore.Images.Media._ID),
            MediaStore.Images.Media.DATA.toString() + "=?",
            arrayOf(imgPath),
            null
        )

        try {
            if (cursor.moveToFirst()) {
                val id: Long = cursor.getLong(0)
                val contentUri: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                val uri: Uri = ContentUris.withAppendedId(contentUri, id)

                val count: Int = activity.contentResolver.delete(uri, null, null)

                if (count > 0) {
                    callBack.onResult(true)
                } else {
                    callBack.onResult(false)
                }
            } else {
                val isSuccess = File(imgPath).delete()
                if (isSuccess) {
                    callBack.onResult(true)
                } else {
                    callBack.onResult(false)
                }
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && e is RecoverableSecurityException) {
                val DELETE_REQUEST_CODE = 1001
                ActivityResultFragment.registerActivityResult(activity,
                    object : ActivityResultFragment.ActivityResultListener {
                        override fun onRequest(fragment: ActivityResultFragment?) {
                            try {
                                fragment!!.startIntentSenderForResult(
                                    e.userAction.actionIntent.intentSender,
                                    DELETE_REQUEST_CODE,
                                    null,
                                    0, 0, 0, null
                                )
                            } catch (sendIntentException: IntentSender.SendIntentException) {
                                sendIntentException.printStackTrace()
                                callBack.onResult(false)
                            }
                        }

                        override fun onActivityResult(
                            requestCode: Int,
                            resultCode: Int,
                            @Nullable data: Intent?
                        ) {
                            if (requestCode == DELETE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
                                deleteFileUri(activity, imgPath, callBack)
                            } else {
                                callBack.onResult(false)
                            }
                        }
                    })
            } else {
                callBack.onResult(false)
            }
        }
    }



    /**
     * 删除文件
     * @return
     */
    fun deleteVideoFileUri(activity: FragmentActivity, imgPath: String, callBack: FileUtilCallback) {
        val cursor: Cursor = MediaStore.Images.Media.query(
            activity.contentResolver,
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            arrayOf<String>(MediaStore.Video.Media._ID),
            MediaStore.Video.Media.DATA.toString() + "=?",
            arrayOf(imgPath),
            null
        )

        try {
            if (cursor.moveToFirst()) {
                val id: Long = cursor.getLong(0)
                val contentUri: Uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
                val uri: Uri = ContentUris.withAppendedId(contentUri, id)

                val count: Int = activity.contentResolver.delete(uri, null, null)

                if (count > 0) {
                    callBack.onResult(true)
                } else {
                    callBack.onResult(false)
                }
            } else {
                val isSuccess = File(imgPath).delete()
                if (isSuccess) {
                    callBack.onResult(true)
                } else {
                    callBack.onResult(false)
                }
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && e is RecoverableSecurityException) {
                val DELETE_REQUEST_CODE = 1001
                ActivityResultFragment.registerActivityResult(activity,
                    object : ActivityResultFragment.ActivityResultListener {
                        override fun onRequest(fragment: ActivityResultFragment?) {
                            try {
                                fragment!!.startIntentSenderForResult(
                                    e.userAction.actionIntent.intentSender,
                                    DELETE_REQUEST_CODE,
                                    null,
                                    0, 0, 0, null
                                )
                            } catch (sendIntentException: IntentSender.SendIntentException) {
                                sendIntentException.printStackTrace()
                                callBack.onResult(false)
                            }
                        }

                        override fun onActivityResult(
                            requestCode: Int,
                            resultCode: Int,
                            @Nullable data: Intent?
                        ) {
                            if (requestCode == DELETE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
                                deleteFileUri(activity, imgPath, callBack)
                            } else {
                                callBack.onResult(false)
                            }
                        }
                    })
            } else {
                callBack.onResult(false)
            }
        }
    }
}
